package com.test;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.DocumentType;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import javax.print.Doc;
import java.io.*;
import java.net.*;
import java.util.concurrent.TimeUnit;


public class Main {

    public static void downloadImage(String imgUrl, String imgName) throws UnsupportedEncodingException {
        String filePath = "d:/image";//图片下载后的存储路径
        String fileName = imgName;//图片名
        File file = new File(filePath + File.separator + fileName + ".jpg");//创建文件
        try {
            URL url = new URL(imgUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setConnectTimeout(10 * 1000);
            DataInputStream dataInputStream = new DataInputStream(urlConnection.getInputStream());//读出
            DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(file));//写入
            byte b[] = new byte[4096];
            int size;
            while ((size = dataInputStream.read(b)) != -1) {
                dataOutputStream.write(b, 0, size);
            }
            dataInputStream.close();
            dataOutputStream.close();
            System.out.println("下载图片：" + (fileName + ".jpg") + "  成功！");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test1() {//下载图片
        try {
            for (int i = 1; i <= 4; i++) {
                System.out.println("第" + i + "页：");
                Document document = Jsoup.connect("https://bing.ioliu.cn/?p=" + i).get();
                Elements elements = document.select(".card.progressive");
                for (Element element : elements) {
                    Spider spider = new Spider(element);
                    Thread t = new Thread(spider);
                    t.start();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Main().test1();
    }

    class Spider implements Runnable {
        Element element;

        public Spider(Element element) {
            this.element = element;
        }

        public void run() {
            System.out.println("下载地址： " + element.select("a.ctrl.download").attr("abs:href"));
            String imgurl = element.select("a.ctrl.download").attr("abs:href").toString();
            String imgname_e1 = element.select(".location em.t").text();//图片拍摄地点
            String imgname_e2 = element.select(".calendar em.t").text();//图片拍摄的时间
            System.out.println("图片拍摄地点：" + imgname_e1 + "   图片拍摄时间：" + imgname_e2);
            try {
                Main.downloadImage(imgurl, (imgname_e1 + imgname_e2));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }



    @Test
    public void test9() throws IOException {

        /*
         * 利用第一个页面链接获得各个书本的链接地址
         * **/
        Document document = Jsoup.connect("http://www.shicimingju.com/book/").timeout(5000).header("Accept-Language", "zh-cn").get();
        Elements elements = document.select(".www-main-container");


        /*获得a标签内的链接
         * **/
        Elements blink = elements.select("a[href]");
        for (Element element : blink) {
            //         System.out.println(element.text());
            /*创建文件根目录
             **/
            String filePath = "E:\\Shixun";
            File file = new File( filePath +  "/" + element.text());
            /*创建文件**/
            file.mkdir();
            //          System.out.println(element.attr("abs:href"));


            /*利用书本链接获得书本内每个章节的链接
             **/
            String url1 = element.attr("abs:href");
            if(url1.equals("http://www.shicimingju.com/book/shishuoxinyu (shishuoxinyu).html")){
                url1 = "http://www.shicimingju.com/book/shishuoxinyu.html";

            }
            //         System.out.println(url1);
            Document document1 = Jsoup.connect(url1).timeout(50000).get();
            Elements elements1 = document1.select(".book-mulu");
            Elements link = elements1.select("a[href]");
            for (Element element1 : link) {
                //             System.out.println(element1.text());


                /*去除链接内的特殊字符并创建.txt文件
                 * **/
                String text = element1.text();
                String text2 = text.replace("?","");
                String text3 = text2.replace("/","");
                String text4 = text3.replace("\"","");
                File file2 = new File(file,text4+ ".txt");
                file2.createNewFile();


//                  System.out.println(element1.attr("abs:href"));
                String url2 = element1.attr("abs:href");
                String url3 = url2.replace(" ","");

//                  System.out.println(url2);


                Document document2 = Jsoup.connect(url3).timeout(50000).get();
                Elements elements2 = document2.select(".www-main-container");
                for (Element element2 : elements2) {
                    //                      System.out.println(element2.text());
                    //                      System.out.println(content.text());
                    Elements content = element2.select(".chapter_content");
                    String s = content.text();

                    FileWriter fw = null;

                    fw = new FileWriter(file2,true);

                    PrintWriter pw = new PrintWriter(fw);
                    pw.println(s);
                    pw.flush();
                    fw.flush();
                    pw.close();
                    fw.close();
                }

            }

            System.out.println("《" +element.text() + "》" +"下载完成" );

        }

    }






}
